package cate.game.play.buff.h;


import cate.game.play.base.HpCfg;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.result.BuffAnounceResult;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass("回合伤害结算BH")
public class 回合伤害结算BH extends BuffHandler {

    @NoteField("血量配置")
    private List<HpCfg> 血量配置 = new ArrayList<>();

    @NoteField("回合开始生效")
    private boolean 回合开始;

    @NoteField("回合结束生效")
    private boolean 回合结束;

    //血量配置=1:1:1000&回合开始=0&回合结束=1
    @Override
    public void parse(String code) {
        PlayCfgArgs args = new PlayCfgArgs(code);
        血量配置.add(new HpCfg(args));
        回合开始 = args.getBoolean("回合开始", false);
        回合结束 = args.getBoolean("回合结束", false);
    }


    @Override
    public void parseExt(String code) {
        PlayCfgArgs args = new PlayCfgArgs(code);
        血量配置.add(new HpCfg(args));
    }



    private double exDamage;

    public void addExDamage(double add){
        this.exDamage+=add;
    }

    @Override
    public void onRoundBegin(ActionCtx action) {
        if (回合开始) {
            settle(action);
        }
    }

    @Override
    public void onRoundEnd(ActionCtx action) {
        if (回合结束) {
            settle(action);
        }
    }

    private void settle(ActionCtx action) {
        if (buff.owner.isDead()) {
            return;
        }
        double totalDamage = 0d;

        for (HpCfg hpCfg : 血量配置) {
            totalDamage += hpCfg.settle(buff.caster, buff.owner);
        }
        if (exDamage > 0) {
            totalDamage += exDamage;
        }
        if (totalDamage <= 0) {
            return;
        }
        buff.owner.attr.buffHurt(action, buff.caster, totalDamage);
        action.addResult(new BuffAnounceResult().pidSet(buff.owner).buffTidSet(buff.tid));
    }
}
